package com.wcity.manage.service.community.activity.impl;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.activity.ActivityVo;
import com.wcity.manage.dao.mapper.community.activity.ActivityMapper;
import com.wcity.manage.dao.model.community.activity.Activity;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.service.community.activity.ActivityService;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.web.community.ActivityAct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区活动
 * Created by hgg on 2016/12/20.
 */
@Service
public class ActivityServiceImpl implements ActivityService{

    private final static Logger log = LoggerFactory.getLogger(ActivityServiceImpl.class);

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 社区活动列表
     * @param queryMap
     * @return
     */
    @Override
    public List<ActivityVo> activityPageList( Map<String, Object> queryMap ) {

        return activityMapper.activityPageList( queryMap );
    }

    /**
     * 删除社区活动
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deleteActivity( Map<String, Object> deleteMap ) {

        return activityMapper.deleteActivity( deleteMap );
    }

    /**
     * 新增社区活动
     * @param activity
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddActivity( Activity activity,String imgUrl,String blockIdStr ) {

        //【1】生成父级社区活动
        Integer parentId = 0;
        String[] blockIds = blockIdStr.split( "," );
        activity.setBlockId( Integer.parseInt( blockIds[0] ) );
        activity.setPid( 0 );
        Integer saveNum = activityMapper.saveAddActivity( activity );
        if( saveNum == 0 ){
            throw new RuntimeException( "新增社区活动失败" );
        }
        parentId = activity.getId();

        //【2】保存关联封面图片
        ImageUrl image = new ImageUrl( null,imgUrl,null,parentId, SaveImageType.COMMUNITY_ACTIVITY );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存社区活动封面图片失败" );
        }

        //【3】执行保存社区活动
        saveAddActivity(activity, blockIds, parentId,imgUrl);
        return 1;
    }

    /**
     * 更新社区活动
     * @param activity
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdateActivity( Activity activity,String imgUrl,String blockIdStr ) {

        Integer parentId = activity.getId();
        String[] blockIds = blockIdStr.split( "," );

        //【1】删除以前社区活动封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId", activity.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_ACTIVITY );
        imageService.deleteImage( deleteMap );

        //【2】删除以前的小区概况
//        Map<String,Object> deleteActivityMap = new HashMap<>();
//        deleteActivityMap.put( "pid",activity.getId() );
//        deleteActivityMap.put( "userId",activity.getUserId() );
//        Integer deleteNum = activityMapper.deleteActivity( deleteActivityMap );
//        if( deleteNum == 0 ){
//            throw new RuntimeException( "删除社区活动失败" );
//        }

        //【3】执行保存社区活动封面
        ImageUrl image = new ImageUrl( null,imgUrl,null,activity.getId(),SaveImageType.COMMUNITY_ACTIVITY );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存社区活动封面图片失败" );
        }

        //【4】执行更新社区活动概况
        activity.setBlockId( Integer.parseInt( blockIds[0] ) );
        Integer updateNum = activityMapper.saveUpdateActivity( activity );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新社区活动失败 ");
        }

        //【5】执行新增社区活动
//        saveAddActivity( activity, blockIds, parentId,imgUrl );
        return 1;
    }

    /**
     * 社区活动详情
     * @param detailMap
     * @return
     */
    @Override
    public ActivityVo activityDetail( Map<String, Object> detailMap ) {

        return activityMapper.activityDetail( detailMap );
    }

    /**
     * 社区活动总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalActivity( Map<String, Object> queryMap ) {

        return activityMapper.totalActivity( queryMap );
    }

    /**
     * 社区活动列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<ActivityVo> activityList( Map<String, Object> queryMap ) {

        return activityMapper.activityList( queryMap );
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return activityMapper.blockIds( queryMap );
    }

    /**
     * 执行保存社区活动
     * @param activity
     * @param blockIds
     * @param parentId
     */
    private void saveAddActivity( Activity activity ,String[] blockIds,Integer parentId,String imgUrl ){

        for ( String id : blockIds ){
            activity.setBlockId( Integer.parseInt( id ) );
            //【1】执行保存
            activity.setPid( parentId );
            Integer saveNum = activityMapper.saveAddActivity( activity );
            if( saveNum == 0 ){
                throw new RuntimeException("保存社区活动失败");
            }


            //【2】执行保存社区活动封面
            ImageUrl image = new ImageUrl( null,imgUrl,null,activity.getId(),SaveImageType.COMMUNITY_ACTIVITY );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存社区活动封面图片失败" );
            }
        }
    }
}
